function getNum() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var num = JSON.parse(this.responseText).data
            if (num === 0) {
                chrome.browserAction.setBadgeText({text: ''});
            } else {
                chrome.browserAction.setBadgeText({text: '' + num});
                chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});

                var isNotify = localStorage.getItem("isNotify");
                if (isNotify === 'false') {
                    notify(num)
                }
            }
        }
    };
    xhttp.open("GET", "https://www.zixiuyun.top/share-api/share/todayNum", true);
    xhttp.send();
}
function twoHoursOutFunc() {
    getNum()
    window.setInterval(function(){
        dateJudgment()
        getNum()
    }, 1000 * 60 * 60 * 2);
}

window.onload = twoHoursOutFunc

//桌面提醒
function notify(num) {
    var title,content;
    if(!title && !content){
        title = "打新提醒";
        content = "今日有 " + num + " 个可申购新债，不要错过哦！";
    }
    var iconUrl = "images/p1.png";

    if (window.webkitNotifications) {
        //chrome老版本
        if (window.webkitNotifications.checkPermission() == 0) {
            var notif = window.webkitNotifications.createNotification(iconUrl, title, content);
            notif.display = function() {}
            notif.onerror = function() {}
            notif.onclose = function() {}
            notif.onclick = function() {this.cancel();}
            notif.replaceId = 'Meteoric';
            notif.show();
            // 已通知
            localStorage.setItem("isNotify", 'true')
        } else {
            window.webkitNotifications.requestPermission($jy.notify);
        }
    }
    else if("Notification" in window){
        // 判断是否有权限
        if (Notification.permission === "granted") {
            var notification = new Notification(title, {
                "icon": iconUrl,
                "body": content,
            });
            // 已通知
            localStorage.setItem("isNotify", 'true')
        }
        //如果没权限，则请求权限
        else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function(permission) {
                // Whatever the user answers, we make sure we store the
                // information
                if (!('permission' in Notification)) {
                    Notification.permission = permission;
                }
                //如果接受请求
                if (permission === "granted") {
                    var notification = new Notification(title, {
                        "icon": iconUrl,
                        "body": content,
                    });
                    // 已通知
                    localStorage.setItem("isNotify", 'true')
                }
            });
        }
    }
}

// 日期判断 记录通知日期，当前日期不是通知日期则更新记录的通知日期并且重置 是否已通知状态
function dateJudgment() {
    var notifyDate = localStorage.getItem("notifyDate");
    var date = new Date().getDate().toString()
    if (!notifyDate || notifyDate != date) {
        localStorage.setItem("notifyDate", date)
        localStorage.setItem("isNotify", 'false')
    }
}